package com.manor.main.login;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.RegexUtils;

import com.manor.common.utils.ToastUtil;
import com.manor.main.R;

/**
 * 账号密码登录
 */
public class PasswordLoginFragment extends Fragment {

    private EditText etPhoneNumber, etPassword;
    public static final String REGEX_PSWD = "^([0-9]|[a-zA-Z]){8,12}$";


    private Context mContext;

    public PasswordLoginFragment() {

    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    public static PasswordLoginFragment newInstance() {
        return new PasswordLoginFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_login_main, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        etPhoneNumber = view.findViewById(R.id.et_phone_number);
        etPassword = view.findViewById(R.id.et_password);
        view.findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etPhoneNumber.getText().toString().trim();
                if (!TextUtils.isEmpty(phone)) {
                    ToastUtil.show("请输入手机号");
                    return;
                }

                if (!RegexUtils.isMatch(SmsLoginFragment.REGEX_PHONE, phone)) {
                    ToastUtil.show("手机格式不正确");
                    return;
                }

                String password = etPassword.getText().toString().trim();
                if (!TextUtils.isEmpty(password)) {
                    ToastUtil.show("请输入密码");
                    return;
                }
                if (!RegexUtils.isMatch(SmsLoginFragment.REGEX_PHONE, phone)) {
                    ToastUtil.show("密码格式不正确");
                    return;
                }
                login(phone, password);
            }
        });

        TextView tvAgreement = view.findViewById(R.id.tv_agreement);
        if (mContext instanceof LoginActivity) {
            ((LoginActivity) mContext).dealAgreement(tvAgreement);
        }

        view.findViewById(R.id.tv_forget_password).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContext instanceof LoginActivity) {
                    FragmentManager fm = ((LoginActivity) mContext).getSupportFragmentManager();
                    BaseVerifyFragment baseVerify = (BaseVerifyFragment) fm.findFragmentByTag(BaseVerifyFragment.class.getSimpleName());
                    if (baseVerify == null) {
                        baseVerify = BaseVerifyFragment.newInstance();
                    }
                    Bundle args = new Bundle();
                    // 已注册忘记密码
                    args.putInt(VCodeVerifyFragment.FLAG_FROM_TYPE, 0);
                    args.putString(BaseVerifyFragment.FLAG_CONTENT, "忘记密码");
                    baseVerify.setArguments(args);
                    FragmentTransaction ft = fm.beginTransaction();
                    if (!baseVerify.isAdded()) {
                        ft.add(R.id.fl_container, baseVerify, BaseVerifyFragment.class.getSimpleName());
                        ft.addToBackStack(BaseVerifyFragment.class.getSimpleName());
                    } else {
                        ft.show(baseVerify);
                    }
                    ft.commit();
                }
            }
        });

        view.findViewById(R.id.tv_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContext instanceof LoginActivity) {
                    FragmentManager fm = ((LoginActivity) mContext).getSupportFragmentManager();
                    BaseVerifyFragment baseVerify = (BaseVerifyFragment) fm.findFragmentByTag(BaseVerifyFragment.class.getSimpleName());
                    if (baseVerify == null) {
                        baseVerify = BaseVerifyFragment.newInstance();
                    }
                    Bundle args = new Bundle();
                    // 1 第一次使用密码登录时进行注册
                    args.putInt(VCodeVerifyFragment.FLAG_FROM_TYPE, 1);
                    args.putString(BaseVerifyFragment.FLAG_CONTENT, "验证手机号");
                    baseVerify.setArguments(args);
                    FragmentTransaction ft = fm.beginTransaction();
                    if (!baseVerify.isAdded()) {
                        ft.add(R.id.fl_container, baseVerify, BaseVerifyFragment.class.getSimpleName());
                        ft.addToBackStack(BaseVerifyFragment.class.getSimpleName());
                    } else {
                        ft.show(baseVerify);
                    }
                    ft.commit();
                }

            }
        });
    }


    private void login(String phone, String password) {

    }
}